package org.eddy.wechar_sys.temp;

import java.awt.*;
import java.util.HashMap;

public class Function2 {
    public static void main(String[] args) {
        Function2 function = new Function2();
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("fun1", 0);
        map.put("fun2", 0);
        map.put("fun3", 0);
        map.get("fun1");
        function.func(map, aa, Boolean.TRUE);

    }

    public static int aa = 0;

    public void func(HashMap hashMap, int i, Boolean bool) {
        Function2 function = new Function2();
        int fun = getfuncount(i, hashMap);
        if (fun == 2 && i == 3) {
            System.out.println("fun3没机会");
            return;
        }

        Object[] obj = run(i, fun, bool);
        i = (int) obj[1];
        bool = (Boolean) obj[0];
        fun = getfuncount(i, hashMap);


        if (i == 1 || i == 0) {
            hashMap.put("fun1", fun + 1);
            function.outmap(hashMap);
            func(hashMap, aa, bool);
        } else if (i == 2) {
            fun = (int) hashMap.get("fun2");
            hashMap.put("fun2", fun + 1);
            function.outmap(hashMap);
            func(hashMap, aa, bool);
        } else if (i == 3) {
            hashMap.put("fun3", fun + 1);
            function.outmap(hashMap);
            func(hashMap, aa, bool);
        }
    }

    private int getfuncount(int i, HashMap hashMap) {
        int fun;
        if (i < 2) {
            fun = (int) hashMap.get("fun1");
        } else if (i == 2) {
            fun = (int) hashMap.get("fun2");
        } else {
            fun = (int) hashMap.get("fun3");
        }
        return fun;
    }

    public void outmap(HashMap map) {
        for (Object i : map.keySet()) {
            System.out.println("key: " + i + " value: " + map.get(i));
        }
    }

    private Object[] run(int i, int fun, Boolean bool) {
        Object[] obj = new Object[2];
        //i: 上次跑的第几个方法， fun ： 跑了几次 bool: 成功与否
        if (i == 0) {
            obj[0] = runf(1);
            obj[1] = i;
            return obj;
        }
        if (fun == 2 && i < 3) {
            obj[0] = runf(i + 1);
            obj[1] = i + 1;
            return obj;

        } else if (bool) {
            obj[0] = runf(i + 1);
            obj[1] = i + 1;
            return obj;
        } else {
            obj[0] = runf(i);
            obj[1] = i;
            return obj;
        }
    }

    private Boolean runf(int i) {
        Boolean bool = Boolean.TRUE;
        if (i < 2) {
            bool = func1();
        }
        if (i == 2) {
            bool = func2();
        }
        if (i == 3) {
            bool = func3();
        }
        return bool;
    }


    public Boolean func1() {
        aa = 1;
        System.out.println("我是方法1");
        int k = (int) (Math.random() * 2) + 1;
        if (k == 1) {
            Boolean bool = Boolean.TRUE;
            System.out.println(bool);
            return bool;
        } else {
            Boolean bool = Boolean.FALSE;
            System.out.println(bool);
            return bool;
        }

    }

    public Boolean func2() {
        aa = 2;
        System.out.println("我是方法2");
        int k = (int) (Math.random() * 2) + 1;
        if (k == 1) {
            Boolean bool = Boolean.TRUE;
            System.out.println(bool);
            return bool;
        } else {
            Boolean bool = Boolean.FALSE;
            System.out.println(bool);
            return bool;
        }

    }

    public Boolean func3() {
        aa = 3;
        System.out.println("我是方法3");
        int k = (int) (Math.random() * 2) + 1;
        if (k == 1) {
            Boolean bool = Boolean.TRUE;
            System.out.println(bool);
            return bool;
        } else {
            Boolean bool = Boolean.FALSE;
            System.out.println(bool);
            return bool;
        }

    }



}
